package cn.lecosa.spark.es.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_FORMAT_INTEGER = "yyyyMMdd";
	public static final String TIME_FORMAT = "HH:mm:ss";
	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String RFC_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
	
	public static final String TIME_FORMAT_FILE = "yyyyMMddHHmmss";

	
	/**
	 * 格式化时间
	 * @param format
	 * @return
	 */
	public static String getFormatTime(String format,Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	/**
	 * 回去前min的时间
	 * 
	 * @param min
	 *            间隔
	 * @return
	 */
	public static Date getBeforeTime(int min) {
		Calendar beforeTime = Calendar.getInstance();
		beforeTime.add(Calendar.MINUTE, -5);
		return beforeTime.getTime();
	}

	/**
	 * 格式化时间
	 * @param date
	 * @param format
	 * @return
	 */
	public static String toString(java.util.Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public static void main(String[] args) {
		System.out.println(toString(getBeforeTime(5), DATETIME_FORMAT));
	}

}
